<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>C#多线程的常识 | 苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="C#多线程的常识 Unity不是不能使用多线程,而是unity的对象都只能在主线程中进行调用  Unity的DebugLog不管前台后台线程,在播放停止或暂停后,还是都会继续执行  Sleep : Thread.Sleep(TimeSpan.FromSeconds(1)); 阻塞线程1秒  Join : t.Join(); 主线程停止执行,等待子线程执行完成后,继续执行  Abort : t.Ab">
<meta property="og:type" content="article">
<meta property="og:title" content="C#多线程的常识">
<meta property="og:url" content="http://dontworryneedmoney.gitee.io/guolianbo/2019/08/31/dontworry/%E6%9D%82%E9%A1%B9/C#%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%B8%B8%E8%AF%86/index.html">
<meta property="og:site_name" content="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒">
<meta property="og:description" content="C#多线程的常识 Unity不是不能使用多线程,而是unity的对象都只能在主线程中进行调用  Unity的DebugLog不管前台后台线程,在播放停止或暂停后,还是都会继续执行  Sleep : Thread.Sleep(TimeSpan.FromSeconds(1)); 阻塞线程1秒  Join : t.Join(); 主线程停止执行,等待子线程执行完成后,继续执行  Abort : t.Ab">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2019-08-31T09:59:02.000Z">
<meta property="article:modified_time" content="2019-11-10T08:28:53.954Z">
<meta property="article:author" content="worry dont">
<meta property="article:tag" content="杂项">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/guolianbo/atom.xml" title="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/guolianbo/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/guolianbo/" id="logo">苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/guolianbo/">Home</a>
        
          <a class="main-nav-link" href="/guolianbo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/guolianbo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://dontworryneedmoney.gitee.io/guolianbo"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="ejs-dontworry/杂项/C#多线程的常识" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2019/08/31/dontworry/%E6%9D%82%E9%A1%B9/C#%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%B8%B8%E8%AF%86/" class="article-date">
  <time datetime="2019-08-31T09:59:02.000Z" itemprop="datePublished">2019-08-31</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      C#多线程的常识
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="C-多线程的常识"><a href="#C-多线程的常识" class="headerlink" title="C#多线程的常识"></a>C#多线程的常识</h1><ul>
<li><p><strong>Unity不是不能使用多线程,而是unity的对象都只能在主线程中进行调用</strong></p>
</li>
<li><p><strong>Unity的DebugLog不管前台后台线程,在播放停止或暂停后,还是都会继续执行</strong></p>
</li>
<li><p>Sleep : <code>Thread.Sleep(TimeSpan.FromSeconds(1));</code> 阻塞线程1秒</p>
</li>
<li><p>Join : <code>t.Join();</code> 主线程停止执行,等待子线程执行完成后,继续执行</p>
</li>
<li><p>Abort : <code>t.Abort();</code> 终止线程(给线程注入ThreadAbortException,导致线程终止,<font color=red>不推荐使用</font>)</p>
</li>
<li><p><code>Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1); //把所有的线程放在单个CPU上运行</code></p>
</li>
<li><p>在Unity中所有的子线程的Priority都是Lowest(不知道为啥)</p>
</li>
<li><p>前台进程就是亲生的,后台线程就是后娘养的,后台进程会随着主线的结束而结束.前台进程会直至执行结束而退出</p>
</li>
<li><p>下面是一些基本的原则：对于一些在后台运行的线程，当程序结束时这些线程没有必要继续运行了，那么这些线程就应该设置为后台线程。比如一个程序启动了一个进行大量运算的线程，可是只要程序一旦结束，那个线程就失去了继续存在的意义，那么那个线程就该是作为后台线程的。而对于一些服务于用户界面的线程往往是要设置为前台线程的，因为即使程序的主线程结束了，其他的用户界面的线程很可能要继续存在来显示相关的信息，所以不能立即终止它们。这里我只是给出了一些原则，具体到实际的运用往往需要编程者的进一步仔细斟酌。</p>
</li>
<li><p>线程传递参数的三种方法  </p>
<ol>
<li>通过类传递参数</li>
<li>Start方法传入参数,当时方法必须有一个Object的参数</li>
<li>lambda表达式传入参数()=&gt;fun(arg)</li>
</ol>
</li>
<li><p>线程锁,lock关键字</p>
<ol>
<li>lock是关键字,指定一个临界区,当一个线程访问时,阻止其他线程进行访问,等于加了一把锁</li>
<li>这个锁就是lock(o)中的o,所以要确保线程锁的正确性,必须保证所有线程访问的o都是同一个,不然各用各的锁,就不存在锁的作用了</li>
<li>lock是Monitor的语法糖</li>
<li>Monitor.TryEnter(obj,timespan)—-timeout之后，就不执行这段代码了。lock可是一直会死等的。</li>
</ol>
</li>
</ul>
<figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">lock</span>(o)&#123;&#125;</span><br><span class="line"><span class="comment">// 等价于</span></span><br><span class="line"><span class="keyword">try</span>&#123; Monitor.Enter(o);&#125;</span><br><span class="line"><span class="keyword">catch</span>&#123;&#125;</span><br><span class="line"><span class="keyword">finally</span>&#123;Monitor.Exit(o);&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2019/08/31/dontworry/%E6%9D%82%E9%A1%B9/C#%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%B8%B8%E8%AF%86/" data-id="ckb6hw6rw001pjcn22ner3xtv" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/guolianbo/2019/09/01/dontworry/%E6%9D%82%E9%A1%B9/%E5%BC%80%E5%8F%91%E6%96%B9%E6%B3%95%E8%AE%BA1.0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          开发方法论1.0
        
      </div>
    </a>
  
  
    <a href="/guolianbo/2019/08/31/dontworry/Unity3d/Unity%E5%BF%AB%E6%8D%B7%E9%94%AE/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Unity快捷键</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/guolianbo/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Unity3d/" rel="tag">Unity3d</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" rel="tag">消防</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" rel="tag">申论</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" rel="tag">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/guolianbo/tags/Algorithm/" style="font-size: 14px;">Algorithm</a> <a href="/guolianbo/tags/Unity3d/" style="font-size: 12px;">Unity3d</a> <a href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" style="font-size: 18px;">杂项</a> <a href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" style="font-size: 16px;">消防</a> <a href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" style="font-size: 10px;">申论</a> <a href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 20px;">随笔</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/02/">February 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/06/">June 2019</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/guolianbo/2020/06/08/dontworry/Unity3d/IObserver%E5%92%8CIObservable%E5%92%8CIDisposable/">IObserver和IObservable和IDisposable</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/28/dontworry/%E6%9D%82%E9%A1%B9/javascript%E7%9A%84%E6%9C%AC%E8%B4%A8/">javascript的本质</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/27/dontworry/%E6%9D%82%E9%A1%B9/%E5%8D%8F%E7%A8%8B%E7%9A%84%E5%8E%9F%E7%90%86/">协程的原理</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/20/dontworry/%E6%9D%82%E9%A1%B9/linux%E4%B8%8B%E7%9A%84nginx%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/">linux下的nginx的基本操作</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/15/dontworry/%E6%9D%82%E9%A1%B9/%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%8A%A8%E8%AF%8D/">常用函数动词</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 worry dont<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/guolianbo/" class="mobile-nav-link">Home</a>
  
    <a href="/guolianbo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/guolianbo/fancybox/jquery.fancybox.css">

  
<script src="/guolianbo/fancybox/jquery.fancybox.pack.js"></script>




<script src="/guolianbo/js/script.js"></script>




  </div>
</body>
</html>